"""
These views decode objects encoded with json and pass them to
the actual worker functions in "jackson.cdn.server_api"

"""
### Start of imports

# This line just better work...
from jackson import debug

# There's debug stuff for the rest
debug.pre_import(__name__)

debug.importing(__name__, 'jackson.extjson.ext_json as json')
import jackson.extjson.ext_json as json

debug.importing(__name__, 'urllib2', 3)
import urllib2

debug.importing(__name__, 'server_api from jackson.cdn')
from jackson.cdn import server_api

debug.importing(__name__, 'HttpResponse from django.http')
from django.http import HttpResponse

debug.post_import(__name__)

### End of imports

def delegate_keys_handler(request):
    context = [__name__, 'delegate_keys_handler()']
    debug.enter(context, None, 2)
    r = json.loads(request.REQUEST['data'])
    s = server_api.delegate_keys_handler(r)
    result = HttpResponse(json.dumps(s), mimetype='application/json')
    debug.leave(context, None)
    return result

def download_content_handler(request):
    pass

def confirm_distribution_handler(request):
    context = [__name__, '(the view) confirm_distribution_handler()']
    debug.enter(context, None)
    r = json.loads(request.REQUEST['data'])
    debug.comment(context, "Unpacked: " + str(r))
    s = server_api.confirm_distribution_handler(r)
    debug.comment(context, "Response: " + str(s))
    json_result = json.dumps(s)
    debug.leave(context, None)
    return HttpResponse(json_result, mimetype='application/json')

### Duplicate???
#def delegate_keys_handler(request):
    #r = json.loads(request.REQUEST['data'])
    #s = server_api.delegate_keys_handler(r)
    #json_result = json.dumps(s)
    #return HttpResponse(json_result, mimetype='application/json')

def get_url_handler(request):
    r = json.loads(request.REQUEST['data'])
    s = server_api.get_url_handler(r)
    json_result = json.dumps(s)
    return HttpResponse(json_result, mimetype='application/json')

def is_hosting_handler(request):
    r = json.loads(request.REQUEST['data'])
    s = server_api.is_hosting_handler(r)
    json_result = json.dumps(s)
    return HttpResponse(json_result, mimetype='application/json')

def poke_handler(request):
    r = json.loads(request.REQUEST['data'])
    s = server_api.poke_handler(r)
    json_result = json.dumps(s)
    return HttpResponse(json_result, mimetype='application/json')

def redirect_key_handler(request):
    r = json.loads(request.REQUEST['data'])
    key = r['key']
    server = r['server']
    s = server_api.redirect_key(key,server)
    json_result = json.dumps(s)
    return HttpResponse(json_result, mimetype='application/json')
